package com.developer.platform.application.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.developer.platform.application.query.assembler.UserAssembler;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.application.query.dto.UserAndRoleDto;
import com.developer.platform.application.query.dto.UserListDto;
import com.developer.platform.domain.User;
import com.developer.platform.infrastructure.data.mapper.UserMapper;
import com.veeker.core.bean.Pageable;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Component
public class UserQueryManager {

    private final UserAssembler userAssembler;
    private final UserMapper userMapper;

    public UserQueryManager(UserAssembler userAssembler,
                            UserMapper userMapper) {
        this.userAssembler = userAssembler;
        this.userMapper = userMapper;
    }


    /**
     * 分页查询用户列表
     *
     * @author ：qiaoliang
     * @param username :    账号
     * @param name :    昵称
     * @param phone :   手机号
     * @param pageable :    分页
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.microorganism.platform.application.query.dto.UserListDto>
     * @date 2020-10-20 12:00
     */
    public ResponseModel<UserListDto> page(String username, String name , String phone , Pageable pageable){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(username),User::getUsername , username);
        queryWrapper.likeRight(StrUtil.isNotBlank(name),User::getName , name);
        queryWrapper.eq(StrUtil.isNotBlank(phone),User::getPhone , phone);
        queryWrapper.orderByDesc(User::getCreateTime);
        return userAssembler.toUserListDtoPage(
                userMapper.selectPage(new Page<>(pageable.getPageNumber(), pageable.getPageSize()),queryWrapper));
    }


    /**
     *  查询用户信息
     *
     * @author ：qiaoliang
     * @param id :  主键
     * @return com.microorganism.platform.application.query.dto.UserDto
     * @date 2020-10-20 13:44
     */
    public UserAndRoleDto info(long id){
        return userAssembler.toUserAndRoleDto(userMapper.selectById(id));
    }


    public int count(){
        return userMapper.selectCount(new LambdaQueryWrapper<>());
    }

}
